package v1

import (
	"gitee.com/cyanbleeze/go-image-management/internal/service"
	"gitee.com/cyanbleeze/go-image-management/pkg/app"
	"gitee.com/cyanbleeze/go-image-management/pkg/errcode"
	"github.com/gin-gonic/gin"
)

type Like struct {
}

func NewLike() Like {
	return Like{}
}

func (l Like) Update(ctx *gin.Context) {
	params := service.ToggleLikeRequest{}
	res := app.NewResponse(ctx)
	valid, errs := app.BindAndValid(ctx, &params)
	if !valid {
		res.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}
	svc := service.New(ctx)
	err := svc.ToggleLike(&params)
	if err != nil {
		res.ToErrorResponse(errcode.ErrorToggleLikeListFail.WithDetails(err.Error()))
		return
	}
	res.ToResponse(gin.H{})
}

func (l Like) Get(ctx *gin.Context) {
	params := service.LikeRequest{}
	res := app.NewResponse(ctx)
	valid, errs := app.BindAndValid(ctx, &params)
	if !valid {
		res.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}
	svc := service.New(ctx)
	like, err := svc.GetLike(&params)
	if err != nil {
		res.ToErrorResponse(errcode.ErrorGetLikeFail.WithDetails(err.Error()))
	}
	res.ToResponse(like)
}
